package kin.system.util;

import java.lang.reflect.Field;
import java.util.Collection;

import org.springframework.expression.spel.SpelEvaluationException;
import org.springframework.expression.spel.SpelMessage;


public class StringUtil {
public static String fristLetterUpperCase(String s){
	if(s.length()>0){
	String s1=s.substring(0, 1).toUpperCase();
	String s2=s.substring(1, s.length());
	
	return s1+s2;
	}else{
		return "";
	}
}

public static String fristLetterLowerCase(String s){
	if(s.length()>0){
	String s1=s.substring(0, 1).toLowerCase();
	String s2=s.substring(1, s.length());
	
	return s1+s2;
	}else{
		return "";
	}
}

public static StringBuilder deleteLastSymbol(StringBuilder s,String symbol) {
	if(s.length()-1==s.lastIndexOf(symbol)){
		s.deleteCharAt(s.length()-1);
	}
	return s;
}
/**
 * 将集合转换成字符串
 * @param c    要转换的集合
 * @param splitSymbol     分隔字符
 * @param fieldName       字段名称(如果泛型是自定义对象类型就必须指定)
 * @param unique         用分隔字符分隔的每个值是否唯一值
 * @return 
 */
public static String collection2String(Collection c,String splitSymbol,String fieldName,Boolean unique) {
	Object[] objs=c.toArray();
	StringBuilder sb=new StringBuilder();
	for(Object obj:objs){
		Class cls=obj.getClass();
	
		
		if(ClassUtil.isBaseDataType(cls)){
			sb.append(String.valueOf(obj)).append(splitSymbol);
		}else{
			try{
		
				if(ClassUtil.isBaseDataType(ClassUtil.getValue(obj, fieldName).getClass())){
					String temp=String.valueOf(ClassUtil.getValue(obj, fieldName));
					if(unique==true){
						if(!sb.toString().contains(temp))
						sb.append(temp).append(splitSymbol);
					}else{
						sb.append(temp).append(splitSymbol);
					}
				}else{
					throw new ClassCastException(ClassUtil.getValue(obj, fieldName).getClass().getName()+" can't cast to be String Type!!");
				}
			
			
			}catch(SpelEvaluationException e ){
				throw new SpelEvaluationException(SpelMessage.PROPERTY_OR_FIELD_NOT_READABLE,fieldName,cls.getName());
				
			}
			
		}
		
	}
	if(sb.length()>0)
	sb=deleteLastSymbol(sb,splitSymbol);
	
	return sb.toString();
	
}

public static Collection string2Collection(String s,String splitSymbol,Class collectionType,Boolean unique){
    Collection c = null;
    if(s!=null&&s!="")
    if(unique){
    	
    }else{
    	
    }
	return c;
	
}

public static boolean isEmpty(String s){
   if (null==s||s.equals("")){
	   return true;
   }
	return false;
	
}


}
